package managers;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;



import entities.Category;

@Stateless
public class manCategory{
	@PersistenceContext
    private EntityManager em;
	
	public boolean addCate(String name){
		boolean flag=false;
		Category nCat=new Category();
		nCat.setName(name);
		em.persist(nCat);
		flag=true;
		return flag;
	}
	public List<Category> allCategory(){
		TypedQuery<Category> request = em.createQuery("select c from Category c ", Category.class);
		if (request==null)
			System.out.println("request null");
		return request.getResultList();
	}  
	public boolean delCategory(int id){
		boolean flag=false;
		Category nCat=em.find(Category.class,id);  
	    em.remove(nCat); 
		flag=true;
		return flag;
	}
	public boolean updateCategory(int id,String name){
		boolean flag=false;
		Category nCat=em.find(Category.class,id);   
        em.clear(); //把实体管理器中的所有实体变成游离状态。  
        nCat.setName(name);
        em.merge(nCat);  
        flag=true;
		return flag;
	}
}